Skip to content

fix(claude): preserve original repository for mutation-scope lifecycle - #359

Merged
davidabram merged 6 commits into
mainfrom
claude-stable-scope-repository-routing
Nov 25, 2026
Merged

davidabram merged 6 commits into
mainfrom
claude-stable-scope-repository-routing

Conversation

@davidabram

@davidabram davidabram commented Nov 23, 2026 •

Copy link
Copy Markdown
Member

Problem

Claude mutation-scope hooks resolved the Git repository separately from each event's cwd. If PreToolUse started in repository A and PostToolUse ran after the session moved into nested repository B, the adapter looked for the attempt in B and left the original scope active in A.

Fix

  • Stable route anchor. The Git directory of $CLAUDE_PROJECT_DIR locates a durable route table (<anchor-git-dir>/sce/claude-mutation-scope-routes.json). A live probe confirmed it stays the same across cwd changes, subagents, and terminal events. The anchor never identifies a mutation target.
  • Per-tool routes. Before Start, PreToolUse persists (session_id, agent_id?, tool_use_id) -> (checkout_path, git_dir). checkout_path is the canonical worktree root of the tool's cwd, and git_dir is resolved from that root. Failing to resolve the root, persist the route (including the directory sync after the rename), or a conflict denies the tool.
  • Routed lifecycle. PostToolUse, PostToolUseFailure, PermissionDenied, Stop/StopFailure/UserPromptSubmit, SubagentStop, SessionEnd, and WorktreeRemove finish attempts in the routed checkout and never scan other repositories. A route is removed only after its attempt is gone.
  • Legacy compatibility. Attempts written before routing (no routed marker) are still found through the event cwd. A routed attempt whose route is missing is never closed through cwd.
  • Durability. Route writes use a lock, a temp file, sync_data, an atomic rename, and a directory sync on Unix. Directory open and sync failures fail the write. A same-key retry syncs the directory again before it reports AlreadyRouted.

No change to spec/mutation_cursor.qnt, the mutation protocol, generic ingress, or migrations. The only mutation_trace change re-exports the existing resolve_worktree_root.

Plan and validation report: context/plans/claude-mutation-scope-stable-repository-routing.md (AC1–AC9, T01–T05).

Validation

  • nix develop -c ./scripts/run-cli-cargo.sh test --manifest-path cli/Cargo.toml: 920 passed, including the Quint MBT connect tests
  • Clippy with -D warnings and cargo fmt --check pass
  • nix flake check: all checks passed
  • Live Claude Code 2.1.309 rerun (T04) with nested repositories: every scope closed in its PreToolUse repository, no active scopes, route table empty

Known limits

  • A legacy attempt whose cwd changed before completion is still orphaned.
  • Accepted same-key race between a terminal event's attempt check and its route removal.
  • A route left by a failed start stays as inert bookkeeping if Claude exits without a later terminal or cleanup event.
  • A tool that starts in B and mutates A is scoped to B (by design: no retargeting).
  • Adapter state writes (state.rs) still treat the directory sync as best-effort.

🤖 Generated with Claude Code

@davidabram
davidabram marked this pull request as ready for review November 24, 2026 14:41
davidabram and others added 5 commits November 24, 2026 15:42
Document the verified stable lookup anchor and fallback policy needed to route Claude mutation attempts across changing hook working directories. Record the probe evidence, project Git-directory anchor, route location, and fail-closed behavior for unresolved project roots.

Plan: claude-mutation-scope-stable-repository-routing; task: T01.

Co-authored-by: SCE <sce@crocoder.dev>
…gnostic

Persist per-tool invocation routes at the stable project Git anchor with atomic, idempotent updates, target-checked removal, and fail-closed table handling. Document the route store and record completion of plan claude-mutation-scope-stable-repository-routing task T02, while preserving the adapter-state lock-timeout diagnostic from the hook run.

Co-authored-by: SCE <sce@crocoder.dev>
Terminal Claude hook events can report a cwd different from PreToolUse, leaving scopes active in the original repository. Persist per-tool routes under the stable project Git directory before Start, and resolve terminal and cleanup events through the recorded target while preserving legacy fallback for older attempts. Add lifecycle coverage and document the routing decision and behavior.

Plan: `claude-mutation-scope-stable-repository-routing`
Task: `T03`

Co-authored-by: SCE <sce@crocoder.dev>
Complete task T04 in the claude-mutation-scope-stable-repository-routing plan by recording the routing validation, live nested-repository rerun, measured hook overhead, and remaining recovery limits. Add local Git identity setup to the worktree-removal fixture so the sandboxed test is self-contained; no production code changes.

Co-authored-by: SCE <sce@crocoder.dev>
@davidabram
davidabram force-pushed the claude-stable-scope-repository-routing branch from 8b4d7e3 to 54b8450 Compare November 24, 2026 14:55
…e route sync errors

PreToolUse stored the raw tool cwd as the route checkout path. When that
cwd was a subdirectory the tool later deleted or renamed, terminal events
drove the seam with a path that no longer existed. PreToolUse now resolves
the canonical worktree root with the existing resolve_worktree_root and
derives the Git directory from that root, so both route fields describe
one checkout, including linked worktrees and nested repositories.

The route store discarded directory open and sync_all failures after the
atomic rename. Both now fail the write with the directory path in the
error, so PreToolUse denies the tool. A same-key, same-target replay
re-syncs the directory before reporting AlreadyRouted. A route left
visible without an attempt is retired by the next terminal or cleanup
event.

Remove the tracked sce-next-task session log and ignore its tmp directory.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

Co-authored-by: SCE <sce@crocoder.dev>
@davidabram
davidabram merged commit e767e65 into main Nov 25, 2026
14 of 15 checks passed
@davidabram
davidabram deleted the claude-stable-scope-repository-routing branch November 25, 2026 09:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant